<template>
	<view class="wrapBgc">
		<search :defaultSearchWord="defaultSearchWord"></search>
		<banner :bannerList="bannerList"></banner>
		<recommend></recommend>
		<recosongsheet :recommendList="recommendList"></recosongsheet>
		<player :curMusicInfo="curMusicInfo"></player>
	</view>
</template>

<script>
import player from '../../components/player.vue'
import search from '../../components/search.vue'
import banner from '../../components/banner.vue'
import recommend from '../../components/recommend.vue'
import recosongsheet from '../../components/recoSongSheet.vue'
	
import { mapState, mapMutations } from 'vuex'
export default {
	data() {
		return {
			hasPlayingMusic: false,
			bannerList: [],
			defaultSearchWord: '',
			recommendList: null
		}
	},
	computed:{
		...mapState(['curTapIndex', 'globalPlayList', 'globalPlayStatus', 'curMusicInfo'])
	},
	methods: {
		...mapMutations(['changePlayStatus', 'changeStopStatus', 'changeGlobalPlayList', 'changeCurMusicInfo', 'changeCurTapIndex']),
		openPlayPage(id){
			uni.navigateTo({
				url: `../play/play?mid=${id}`
			})
		}
	},
	components:{
		player, search, banner, recommend, recosongsheet
	},
	onLoad() {
		let that = this
		console.log('that:',that);
		this.$http.getBanner(this.bannerList)
		let userInfo = uni.getStorageSync('userInfo')
		// console.log('userInfo:', userInfo);
		
		
		// 请求推荐歌单
		if(userInfo) {
			uni.request({
				url: `${that.$http.vercelUrl}/personalized?limit=6`,
				header:{
					cookie: userInfo.cookie
				},
				success(res) {
					// console.log('登录推荐歌单：',res.data);
					let recommendInfoList = res.data.result
					that.recommendList = []
					recommendInfoList.forEach((item, index, array) => {
						that.recommendList[index] = {
							listId: array[index].id,
							listName: array[index].name,
							picUrl: array[index].picUrl,
							playCount: array[index].playCount
						}
					})
					// console.log('登录推荐歌单(已处理)：', that.recommendList);
				},
				fail(e) {
					console.log('获取登录推荐歌单失败：', e);
				}
			})
		} else {
			uni.request({
				url: `${that.$http.vercelUrl}/personalized?limit=6`,
				success(res) {
					console.log('未登录推荐歌单：', res.data);
					let recommendInfoList = res.data.result
					that.recommendList = []
					recommendInfoList.forEach((item, index, array) => {
						that.recommendList[index] = {
							listId: array[index].id,
							listName: array[index].name,
							picUrl: array[index].picUrl,
							playCount: array[index].playCount
						}
					})
				},
				fail(e) {
					console.log('获取未登录推荐歌单失败：', e);
				}
			})
		}
		
		// 监听播放事件
		this.$player.onPlay(function(){
			let pages = getCurrentPages()
			let currentPage = pages[pages.length - 1]
			console.log('当前页面栈：', currentPage.route);
			if (currentPage.route !== 'pages/play/play' && currentPage.route !== 'pages/discovery/discovery'){
				let mid = that.curMusicInfo.mid
				that.openPlayPage(mid)
			}
			// console.log('curTapIndex:', that.curTapIndex);
		})
		
		// 监听播放结束事件
		this.$player.onEnded(function(){
			that.changeStopStatus()
			that.playStatus = that.globalPlayStatus
			console.log('播放器已结束播放');
		})
		
		// 监听播放错误事件
		this.$player.onError(function(e){
			uni.showToast({
				title: '开通VIP即可畅听',
				icon: 'none',
				success() {
					that.changeStopStatus()
				}
			})
			console.log('播放错误',e);
		})
	},
	onShow() {
		// 每次页面显示时获取默认搜索词
		let that = this
		uni.request({
			url: `${that.$http.vercelUrl}/search/default`,
			success: function(res) {
				that.defaultSearchWord = res.data.data.realkeyword
			}
		})
	}
	
}
</script>

<style scoped>

</style>
